package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.ShippingAddressActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.ShippingAddressModel;
import com.diudiutechnology.www.yadiudiu.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/22.
 */

public class ShippingAddressActivityAdapter  extends BaseAdapter {

    private Context mContext;

    List<ShippingAddressModel> model= new ArrayList<>();
    public ShippingAddressActivityAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return model.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    public void addItem( List<ShippingAddressModel> model){

        this.model=model;
        notifyDataSetChanged();
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.itme_shipping_address, null);
            holder.imte_myaddress_text = (TextView) convertView
                    .findViewById(R.id.imte_myaddress_text);
            holder.imte_name = (TextView) convertView
                    .findViewById(R.id.imte_name);
            holder.imte_myaddress_arrow = (LinearLayout) convertView
                    .findViewById(R.id.imte_myaddress_arrow);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final ShippingAddressModel shippingAddressModel = model.get(position);
        String detailedadd="";
        if (!Sundry.getStinges(shippingAddressModel.getDetailedadd())){
            String[] split = shippingAddressModel.getDetailedadd().split(",");
            for (int i=0;i<split.length;i++){
                    detailedadd=detailedadd+split[i];
            }
        }
        holder.imte_myaddress_text.setText(detailedadd);
        String sex="";
        if (shippingAddressModel.getSex().equals("2")){
            sex="先生";
        }else {
            sex="女士";
        }
        holder.imte_name.setText(shippingAddressModel.getConsignee()+"("+sex+")");
        holder.imte_myaddress_arrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((ShippingAddressActivity)mContext).processmodification(position);
            }
        });
        return convertView;
    }

    class ViewHolder {


        LinearLayout imte_myaddress_arrow;
        TextView imte_name;
        TextView imte_myaddress_text;

    }
}

